/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2020. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

#ifndef _AC780X_TIMER_REG_H
#define _AC780X_TIMER_REG_H
/*!
 * @file ac780x_timer_reg.h
 *
 * @brief Timer module access register inline function definition.
 *
 */
#ifdef __cplusplus
extern "C" {
#endif

/* ===========================================  Includes  =========================================== */
#include "ac780x_timer.h"

/* ============================================  Define  ============================================ */

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */

/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */
/*!
 * @brief Enable/disable timer module.
 *
 * This function enable the TIMER module.
 * It should be called before setup any timer channel.
 *
 * @param[in] state: enabling state
 *               - ENABLE
 *               - DISABLE
 * @return none
 */
__STATIC_INLINE void TIMER_Enable(ACTION_Type state)
{
    MODIFY_REG32(TIMER_CTRL->CR, TIMER_CTRL_TIMER_CR_MC_EN_Msk, TIMER_CTRL_TIMER_CR_MC_EN_Pos, state);
}

/*!
 * @brief Sets the Timer input Clock selection.
 *
 * @param[in] clocksel: the clock cource to be configured
 * @return none
 */
__STATIC_INLINE void TIMER_SetClockSelect(const TIMER_ClkSrcType clockSel)
{
    MODIFY_REG32(TIMER_CTRL->CR, TIMER_CTRL_TIMER_CR_PCS_Msk, TIMER_CTRL_TIMER_CR_PCS_Pos, clockSel);
}

/*!
 * @brief Gets the Timer input Clock selection.
 *
 * @return The Clock source of Timer
 */
__STATIC_INLINE TIMER_ClkSrcType TIMER_GetClockSelect(void)
{
    return ((TIMER_ClkSrcType)(READ_BIT32(TIMER_CTRL->CR, TIMER_CTRL_TIMER_CR_PCS_Msk) >> TIMER_CTRL_TIMER_CR_PCS_Pos));
}

/*!
 * @brief Gets the interrupt flag of timer channel.
 *
 * @param[in] mask: The interrupt flag getting mask
 * @return The interrupt flag of timer channels
 */
__STATIC_INLINE uint32_t TIMER_GetInterruptFlag(uint32_t mask)
{
    return READ_BIT32(TIMER_CTRL->SR, mask);
}

/*!
 * @brief Clears the interrupt flag of timer channel.
 *
 * @param[in] mask: The interrupt flag clearing mask
 * @return None
 */
__STATIC_INLINE void TIMER_ClearInterruptFlag(uint32_t mask)
{
    WRITE_REG32(TIMER_CTRL->SR, mask);
}

/*!
 * @brief Enable/disable timer channel interrupt.
 *
 * @param[in] mask: The interrupt enabling mask
 
 * @return none
 */
__STATIC_INLINE void TIMER_EnableInterrupt(uint32_t mask)
{
    SET_BIT32(TIMER_CTRL->IER, mask);
}

/*!
 * @brief Disables the interrupt for timer channel.
 *
 * @param[in] mask: The interrupt disabling mask
 * @return None
 */
__STATIC_INLINE void TIMER_DisableInterrupt(uint32_t mask)
{
    CLEAR_BIT32(TIMER_CTRL->IER, mask);
}

/*!
 * @brief Start timer channel.
 *
 * @param[in] mask: Timer channels starting mask
 * @return none
 */
__STATIC_INLINE void TIMER_StartChannel(uint32_t mask)
{
    SET_BIT32(TIMER_CTRL->ENR, mask);
}

/*!
 * @brief Stops the timer channel from counting.
 *
 * @param[in] mask: Timer channels stopping mask
 * @return None
 */
__STATIC_INLINE void TIMER_StopChannel(uint32_t mask)
{
    CLEAR_BIT32(TIMER_CTRL->ENR, mask);
}

/*!
 * @brief Set timer channel period load value.
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 * @param[in] value: period value
 * @return none
 */
#define TIMER_SetPeriodLoadValue(TIMERCHx, value)      ((TIMERCHx)->TVAL = (value))

/*!
 * @brief Get timer channel period load value.
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 * @return Timer channel period value
 */
#define TIMER_GetPeriodLoadValue(TIMERCHx)      ((TIMERCHx)->TVAL)

/*!
 * @brief Get the current timer channel counting value.
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 * @return Current timer channel counting value
 */
#define TIMER_GetCurrentValue(TIMERCHx)    ((TIMERCHx)->CVAL)

/*!
 * @brief Clear the current timer channel counting value.
 *
 * @param[in] TIMERCHx: timer channel
                 - TIMER_CHANNEL0
                 - TIMER_CHANNEL1
                 - TIMER_CHANNEL2
                 - TIMER_CHANNEL3
 * @param[in] value: clear cval number,any number can clear
 * @return none
 */
#define TIMER_ClearCurrentValue(TIMERCHx, value)    ((TIMERCHx)->CVAL = (value))

/*!
 * @brief Sets operation mode of timer channel.
 *
 * @param[in] TIMERCHx: timer channel
 *                  - TIMER_CHANNEL0
 *                  - TIMER_CHANNEL1
 *                  - TIMER_CHANNEL2
 *                  - TIMER_CHANNEL3
 * @param[in] mode: Operation mode of timer channel
 * @return None
 */
__STATIC_INLINE void TIMER_SetChannelModeCmd(TIMER_CHANNEL_Type * const TIMERCHx, TIMER_ModeType mode)
{
    MODIFY_REG32(TIMERCHx->Ctrl, TIMER_CHANNEL_Channel_Ctrl_MS_Msk, TIMER_CHANNEL_Channel_Ctrl_MS_Pos, mode);
}

/*!
 * @brief Sets Pluse captrue polarity.
 *
 * @param[in] TIMERCHx: timer channel
 *                  - TIMER_CHANNEL0
 *                  - TIMER_CHANNEL1
 *                  - TIMER_CHANNEL2
 *                  - TIMER_CHANNEL3
 * @param[in] pol: capture pin pulse polarity
 * @return None
 */
__STATIC_INLINE void TIMER_SetPulsePolarity(TIMER_CHANNEL_Type * const TIMERCHx, TIMER_PulsePolarityType pol)
{
    MODIFY_REG32(TIMERCHx->Ctrl, TIMER_CHANNEL_Channel_Ctrl_PPS_Msk, TIMER_CHANNEL_Channel_Ctrl_PPS_Pos, pol);
}

/*!
 * @brief Sets Synchronous cval read value.
 *
 * @param[in] TIMERCHx: timer channel
 *                  - TIMER_CHANNEL0
 *                  - TIMER_CHANNEL1
 *                  - TIMER_CHANNEL2
 *                  - TIMER_CHANNEL3
 * @param[in] ssvalue: the Synchronous cval read value
 * @return none
 */
__STATIC_INLINE void TIMER_SetSELValue(TIMER_CHANNEL_Type* const TIMERCHx, const TIMER_Sel ssvalue)
{
    MODIFY_REG32(TIMERCHx->Ctrl, TIMER_CHANNEL_Channel_Ctrl_SEL_Msk, TIMER_CHANNEL_Channel_Ctrl_SEL_Pos, (uint32_t)ssvalue);
}

/*!
 * @brief Set timer channel link mode.
 *
 * timer channel 0 cannot be link
 *
 * @param[in] TIMERCHx: timer channel
                 - TIMER_CHANNEL0
                 - TIMER_CHANNEL1
                 - TIMER_CHANNEL2
                 - TIMER_CHANNEL3
 * @return none
 */
__STATIC_INLINE void TIMER_SetChainMode(TIMER_CHANNEL_Type * const TIMERCHx, ACTION_Type state)
{
    MODIFY_REG32(TIMERCHx->Ctrl, TIMER_CHANNEL_Channel_Ctrl_CHN_Msk, TIMER_CHANNEL_Channel_Ctrl_CHN_Pos, state);
}

/*!
 * @brief Sets if the prescaler/filter is bypassed.
 *
 * @param[in] TIMERCHx: timer channel
 *                  - TIMER_CHANNEL0
 *                  - TIMER_CHANNEL1
 *                  - TIMER_CHANNEL2
 *                  - TIMER_CHANNEL3
 * @param[in] enable: the prescaler/filter bypass state to be configured
 * @return none
 */
__STATIC_INLINE void TIMER_SetBypass(TIMER_CHANNEL_Type* const TIMERCHx, const ACTION_Type enable)
{
    MODIFY_REG32(TIMERCHx->Ctrl, TIMER_CHANNEL_Channel_Ctrl_BYP_Msk, TIMER_CHANNEL_Channel_Ctrl_BYP_Pos, enable);
}

/*!
 * @brief Gets if the prescaler/filter is bypassed.
 *
 * @param[in] TIMERCHx: timer channel
 *                  - TIMER_CHANNEL0
 *                  - TIMER_CHANNEL1
 *                  - TIMER_CHANNEL2
 *                  - TIMER_CHANNEL3
 * @return The prescaler/filter bypass state
            DISABLE:disable
            ENABLE: enable
 */
__STATIC_INLINE ACTION_Type TIMER_GetBypass(TIMER_CHANNEL_Type* const TIMERCHx)
{
    return ((ACTION_Type)((READ_BIT32(TIMERCHx->Ctrl, TIMER_CHANNEL_Channel_Ctrl_BYP_Msk) >> TIMER_CHANNEL_Channel_Ctrl_BYP_Pos) != DISABLE));
}

/*!
 * @brief Sets the prescaler/filter value.
 *
 * @param[in] TIMERCHx: timer channel
 *                  - TIMER_CHANNEL0
 *                  - TIMER_CHANNEL1
 *                  - TIMER_CHANNEL2
 *                  - TIMER_CHANNEL3
 * @param[in] presc: the Prescaler value to be configured
 * @return none
 */
__STATIC_INLINE void TIMER_SetPrescaler(TIMER_CHANNEL_Type* const TIMERCHx, const TIMER_PrescalerType presc)
{
    MODIFY_REG32(TIMERCHx->Ctrl, TIMER_CHANNEL_Channel_Ctrl_PSVAL_Msk, TIMER_CHANNEL_Channel_Ctrl_PSVAL_Pos, (uint32_t)presc);
}

/*!
 * @brief Gets the prescaler/flter value.
 *
 * @param[in] TIMERCHx: timer channel
 *                  - TIMER_CHANNEL0
 *                  - TIMER_CHANNEL1
 *                  - TIMER_CHANNEL2
 *                  - TIMER_CHANNEL3
 * @return The prescaler/filter value
 */
__STATIC_INLINE TIMER_PrescalerType TIMER_GetPrescaler(TIMER_CHANNEL_Type* const TIMERCHx)
{
    return ((TIMER_PrescalerType)(READ_BIT32(TIMERCHx->Ctrl, TIMER_CHANNEL_Channel_Ctrl_PSVAL_Msk) >> TIMER_CHANNEL_Channel_Ctrl_PSVAL_Pos));
}

#ifdef __cplusplus
}
#endif

#endif /* _AC780X_TIMER_REG_H */

/* =============================================  EOF  ============================================== */
